package BESA.util;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

public class ConfigTransactionalBESA {

	static private String GCALocation;
	static private String GCAPort;

	/**
	 * Here we're going to read the configuration file, this file should contain
	 * the ip address of the GCA and the port. The GCA will work with RMI
	 */

	static {
		readFile();
	}

	/**
	 * @return the IP address of the GCA
	 */
	public static String getGCALocation() {
		return GCALocation;
	}

	public static void setGCALocation(String gCALocation) {
		GCALocation = gCALocation;
	}

	public static void setGCAPort(String gCAPort) {
		GCAPort = gCAPort;
	}

	/**
	 * @return the port of the GCA
	 */
	public static int getGCAPort() {
		return Integer.parseInt(GCAPort);
	}

	public static String getGCA_URL() {
		// TODO Auto-generated method stub
		return "rmi://" + GCALocation + ":" + GCAPort + "/GCA";
	}

	public static void readFile(){
		try {

			BufferedReader br = new BufferedReader(new FileReader(
					"ConfigTransactionalBESA.propierties"));
			String line = "";
			while ((line = br.readLine()) != null) {
				if (GCALocation == null || GCAPort == null) {
					if (line.startsWith("GCALocation")) {
						GCALocation = line.substring(line.lastIndexOf('=')+1);
					} else if (line.startsWith("GCAPort")) {
						GCAPort = line.substring(line.lastIndexOf('=')+1);
					}
				} else {
					break;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void writeFile(){
		try {
			PrintWriter pw = new PrintWriter("ConfigTransactionalBESA.propierties");
			pw.println("GCALocation="+GCALocation);
			pw.println("GCAPort="+GCAPort);
			pw.flush();
			pw.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
